

local h232 = {}

local h232_data = {
    rbuff = {},
    rbuff_len = 0,
    strbuff = "",
}
local h232_cfg = {
    uid = 1,
    baud = 115200,
    data = 8,
    stop = 1,
    parity = uart.NONE,
}
-------------------------业务代码-----------------------
-- function serial2dtu(data,res)
--     local keychars={0xfe,0xfd}
--     local keychars_replace={0xeefd,0xedfd}
--     res=""
--     for i=1,#data,1 do
--         if (data:byte(i) == keychars[1]) then
--             res=res..keychars_replace[1]
--         elseif (data:byte(i) == keychars[2]) then
--             res=res..keychars_replace[2]
--         else
--             res=res..data:sub(i,1)
--         end
--     end
-- end
function h232.serial2dtu(data)
    local keychars = { 0xfe, 0xfd }
    local keychars_replace = { 0xfd, 0xee, 0xfd, 0xed }
    local res = ""
    if #data == 0 then
        return res
    end
    for i = 1, #data, 1 do
        if data:byte(i) == keychars[1] then
            res = res .. string.format("%02x", keychars_replace[1])
            res = res .. string.format("%02x", keychars_replace[2])
        elseif data:byte(i) == keychars[2] then
            res = res .. string.format("%02x", keychars_replace[3])
            res = res .. string.format("%02x", keychars_replace[4])
        else
            res = res .. string.format("%02x", data:byte(i))
        end
    end
    return string.fromHex(res)
end

-------------------------串口-------------------------
function h232.write(s)
    -- log.info("uart1 write",s:toHex())
    uart.write(1, s)
end

-- 串口接收数据处理任务
local function h232_read()
    local cacheData = ""
    local s
    repeat
        s = uart.read(h232_cfg.uid, 1)
        if #s > 0 then
            cacheData = cacheData .. s
        else
            if cacheData:len() > 0 then
                local a, _ = string.toHex(cacheData)
                log.info("[h232]->rev:", #cacheData)
                -- 用户逻辑处理代码
                table.insert(h232_data.rbuff, cacheData)
                h232_data.rbuff_len = h232_data.rbuff_len + #cacheData
                if h232_data.rbuff_len > 29200 then
                    h232_data.rbuff = {}
                    h232_data.rbuff_len = 0
                end
                h485.Sensor_val.rs232_flag = 1
                h485.Sensor_val.rs232_flag_recheck = 1
                cacheData = ""
            end
        end
    until s == ""
end

uart.on(h232_cfg.uid, "receive", h232_read)
--配置并且打开串口
uart.setup(h232_cfg.uid, h232_cfg.baud, h232_cfg.data, h232_cfg.stop)

-- 启动232串口数据接收任务
-- sys.taskInit(h232_read)

-- sys.timerLoopStart(function ()
--     log.info("[h232]->send test")
--     uart.write(h232_cfg.uid, "123")
-- end,1000)

sys.taskInit(function()
    local send_buff = ""
    while true do
        if h232_data.rbuff_len > 0 then
            h232_data.strbuff = table.concat(h232_data.rbuff)
            h232_data.rbuff = {}
            h232_data.rbuff_len = 0
            if h485.cmd_parse(h232_data.strbuff) == 0 then
                send_buff = h232.serial2dtu(h232_data.strbuff)
                if send_buff ~= "" then
                    log.debug("[h232]->send buff:", #send_buff)
                    hsocket.hsocket_send(send_buff,0,1)
                    -- tf.tf_write_data("[h232]",send_buff)
                    send_buff = ""
                    h232_data.strbuff = ""
                end
            else
                h232.write(h485.print_hconfig_local())
            end
        end
        sys.wait(1000)
    end
end)

return h232
-------------------------看门狗-------------------------
-- --[[
-- 函数名：eatSoftDog
-- 功能  ：喂狗
-- 参数  ：无
-- 返回值：无
-- ]]
-- function eatSoftDog()
--     log.info("eatSoftDog.")
--     rtos.eatSoftDog()
-- end
-- --[[
-- 函数名：closeSoftDog
-- 功能  ：关闭软狗
-- 参数  ：无
-- 返回值：无
-- ]]
-- local function closeSoftDog()
--     print("closeSoftDog test")
--     sys.timerStop(eatSoftDog)
--     rtos.closeSoftDog()
-- end
-- --打开并设置软狗超时时间单位MS,超过设置时间没去喂狗，重启模块
-- rtos.openSoftDog(60 * 1000)
-- --定时喂狗
-- -- sys.timerLoopStart(eatSoftDog, 10 * 1000)
